package com.df.springmvc.workflow.deploy.controller;

import java.io.File;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;
import com.df.springmvc.common.bean.MsgBean;
import com.df.springmvc.common.util.WebUtil;
import com.df.springmvc.supper.SupperController;
import com.df.springmvc.workflow.deploy.services.DeployManageServices;

@Controller
@RequestMapping("/workflow/deploy/DeployManage")
/**
 * 本类仅做一些简单数据处理如:
 *格式验证
 *数据封装
 * @author Turner
 *
 */
public class DeployManageController extends SupperController{
	String page="workflow/deploy/" + this.getClass().getSimpleName().replace("Controller","") + "_";
	@Autowired
	DeployManageServices services;
	@Autowired
	MessageSource messageSource;
	
	public DeployManageController(){
		// TODO Auto-generated constructor stub
	}
	@Override
	@RequestMapping("/")
	public String excute(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		return page;
	}
	@Override
	@RequestMapping("/query")
	public void query(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			HashMap<String,String> paramMap=getParamMap(request,"map");
			Object rs=services.query(getParam_PagingBean(request),paramMap);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
		super.query(model,request,response);
	}
	/**
	 * 查询TaskId关联的的URl
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping(value={"/queryFormInfo"})
	public void queryFormInfo(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			Object obj=services.queryFormInfo(getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(obj));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value={"/" + toAdd})
	public String toAdd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		return page + Add;
	}
	@RequestMapping(value={"/" + toDel})
	public String toDel(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		System.out.println("" + services);
		return page + Del;
	}
	@RequestMapping("/toConfig")
	public String toConfig(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		HashMap<String,String> paramMap=getParamMap(request,"map");
		Object rs;
		try{
			rs=services.queryFlowJson(paramMap);
			addRequestAttribute("info",rs);
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// WebApplicationContext wc=WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		// RequestMappingHandlerMapping rmhp=wc.getBean(RequestMappingHandlerMapping.class);
		return page + Config;
	}
	@Override
	@RequestMapping("/doConfig")
	public void doConfig(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			HashMap<String,String> paramMap=getParamMap(request,"map");
			Object rs=services.doConfig(paramMap);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
		super.doConfig(model,request,response);
	}
	@RequestMapping("/doAuto")
	public void doAuto(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			HashMap<String,String> paramMap=getParamMap(request,"map");
			Object rs=services.doAuto(paramMap);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
		super.doConfig(model,request,response);
	}
	@Override
	@RequestMapping("/toView")
	public String toView(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			String depId=getParamMap(request,"map").get("depId");
			Object rs=services.view(depId);
			addRequestAttribute("info",rs);
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
		}finally{
		}
		return page + View;
	}
	@RequestMapping(value={"/" + doAdd})
	public void doAdd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// WebUtil.json(response,new MsgBean("OK"));
		try{
			Object rs=services.add(getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
	}
	/**
	 * suspend
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/suspend")
	public void suspend(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			Object rs=services.suspend(getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	/**
	 * active
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/active")
	public void active(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			Object rs=services.active(getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	@RequestMapping(value={"/" + doDel})
	public void doDel(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// WebUtil.json(response,new MsgBean("OK"));
		try{
			Object rs=services.del(getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
	}
	@RequestMapping("/" + "getPicPreview")
	public void getPicPreview(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			System.out.println("================>" + model.values());
			// Object rs=services.del(getParamMap(request,"wfFile"));
			File is=services.getPicPreview(request.getParameter("path"),WebUtils.getRealPath(request.getServletContext(),"/"));
			WebUtil.json(response,new MsgBean(is.getAbsolutePath()));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
	}
}
